package algorithm.beforestart.primary.design;

public class MinStack {

    private ListNode head;

    public MinStack() {

    }

    public void push(int val) {
        if(head==null){
            head =new ListNode(val,val,null);
        }else{
            ListNode next =head;
            int min= (head.min<=val)?head.min:val;
            head =new ListNode(val,min,next);
        }

    }

    public void pop() {
        if(head!=null){
            head=head.next;
        }

    }

    public int top() {
        if(head!=null){
            return head.val;
        }else{
            return 0;
        }

    }

    public int getMin() {
        if(head!=null){
            return head.min;
        }else{
            return 0;
        }
    }

    class ListNode {
        public int val;
        public int min;//最小值
        public ListNode next;

        public ListNode(int val, int min, ListNode next) {
            this.val = val;
            this.min = min;
            this.next = next;
        }
    }
}
